#include <unistd.h>
#include <sys/wait.h>

#define PORT 9999

#include "hv/hv.h"
#include "server.h"
#include "client.h"

int main(int argc, char *argv[]) {
    pid_t pid, spid, cpid;

    logger_set_handler(hlog, stdout_logger);
    logger_enable_color(hlog, 1);

    if ((spid = fork()) == 0) {
        smain(PORT);
    } else {
        if ((cpid = fork()) == 0) {
            cmain(PORT);
        } else {
            int status, i;

            for (i = 0; i < 2; i++) {
                pid = wait(&status);

                if (!WIFEXITED(status)) {
                    LOGE("Child %d did NOT exit successfully！(server: %d, client: %d)", pid, spid, cpid);

                    if (WIFSIGNALED(status))
                        LOGE("Killed by signal: %d", WTERMSIG(status));
                }
            }

            LOGI("All Finished.");
        }
    }

    return 0;
}
